//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using System;
using System.Collections.Generic;
using System.Windows.Forms;
using GuidanceExplorer.Model;
using GuidanceExplorer.Services;

namespace GuidanceExplorer.Views
{
    public partial class PickGuidanceItem : UserControl
    {
        private GuidanceItem[] _guidanceItems;
        /// <summary>
        /// Keep the reference to the storage service.
        /// </summary>
        private IModelStorageService _modelService;

        public IModelStorageService ModelService
        {
            set { _modelService = value; }
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public PickGuidanceItem()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initialize the layout of the control within a form.
        /// </summary>
        protected override void InitLayout()
        {
            base.InitLayout();

            if (!this.DesignMode)
            {
                //Set buttons in the parent form.
                Form parent = Parent as Form;
                if (parent != null)
                {
                    parent.CancelButton = btnCancel;
                    parent.AcceptButton = btnOk;
                }

                listView.ModelService = _modelService;

                //Show the contents of the entire guidance
                listView.ShowContents(_modelService.Guidance, _modelService.Guidance.DefaultColumns);

                //Populate combo

                SortedList<string, string> types = new SortedList<string, string>();

                foreach (GuidanceType type in GuidanceType.AllGuidanceTypes)
                {
                    if (string.IsNullOrEmpty(type.NodeDisplayName))
                    {
                        types.Add(type.ListDisplayName, type.ListDisplayName);
                    }
                    else if (!types.ContainsKey(type.NodeDisplayName))
                    {
                        types.Add(type.NodeDisplayName, type.NodeDisplayName);
                    }
                }

                foreach (string type in types.Values)
                {
                    cmbTypes.Items.Add(type);
                }

                cmbTypes.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// Get the current guidance item selected.
        /// </summary>
        public GuidanceItem[] GuidanceItems
        {
            get
            {
                return _guidanceItems;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Form parent = Parent as Form;
            if (parent != null)
            {
                parent.DialogResult = DialogResult.Cancel;
                parent.Close();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            _guidanceItems = listView.GetSelectedGuidanceItems();
            Form parent = Parent as Form;
            if (parent != null)
            {
                parent.DialogResult = DialogResult.OK;
                parent.Close();
            }
        }

        private void listView_GuidanceItemOpened(object sender, GuidanceItemFileReferenceEventArgs e)
        {
            _guidanceItems = new GuidanceItem[1];
            _guidanceItems[0]= e.GuidanceItemFileReferences[0];
            btnOk.PerformClick();
        }

        private void cmbTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<GuidanceType> availableTypes = GuidanceType.AllGuidanceTypes;
            List<string> selectedTypes = new List<string>(availableTypes.Count);
            GuidanceItemCollection guidanceItems = GuidanceItemCollection.New();
            string selectedLabel = (string)cmbTypes.SelectedItem;

            foreach (GuidanceType type in availableTypes)
            {
                if (string.Compare(type.ListDisplayName, selectedLabel) == 0 ||
                    (!string.IsNullOrEmpty(type.NodeDisplayName) && string.Compare(type.NodeDisplayName, selectedLabel) == 0))
                {
                    selectedTypes.Add(type.Name);
                }
            }

            foreach (GuidanceItem item in _modelService.Guidance.Items)
            {
                if (selectedTypes.Contains(item.GuidanceType.Name))
                {
                    guidanceItems.Add(item.Id, item);
                }
            }

            listView.ShowContents(guidanceItems, _modelService.Guidance.DefaultColumns);
        }
    }
}

